package com.xiaotu.spider.project.pageprocessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 YouKuProjectPageProcessor
 * @日期 2017年3月25日
 * @作者 高海军
 * @功能
 */
@Component("YouKuProjectPageProcessor")
public class YouKuProjectPageProcessor extends APageProcessor
{
	
	@SuppressWarnings("deprecation")
	@Override
	protected void setSiteRequest()
	{
		String[] yearArr = DateUtils.getRecentYears(3);
		// String[] areaArr = new String[] { "大陆", "日本", "美国", "英国", "韩国" };
		String[] areaArr = new String[] { "韩国" };
		List<String[]> typeList = new ArrayList<String[]>();
		// typeList.add(new String[] { "", "_g_优酷出品" });
		// typeList.add(new String[] { "" });
		// typeList.add(new String[] { "" });
		// typeList.add(new String[] { "" });
		typeList.add(new String[] { "" });
		
		for (String year : yearArr)
		{
			for (int i = 0; i < areaArr.length; i++)
			{
				String[] typeArr = typeList.get(i);
				for (String type : typeArr)
				{
					String fileName = "D:\\youku\\" + areaArr[i] + "_" + type
							+ ".csv";
					type = StringUtils.isEmpty(type) ? type
							: KeywordEncode.encode(type);
					this.site
							.addStartRequest(this
									.getRequest(
											"http://list.youku.com/category/show/c_97_a_"
													+ KeywordEncode.encode(
															areaArr[i])
													+ "_r_" + year + "_s_1_d_1"
													+ type
													+ ".html?spm=a2h1n.8251845.filterPanel.5!2~1~3!5~A",
											fileName)
									.setPriority(areaArr.length - i));
				}
			}
		}
	}
	
	private Request getRequest(String url, String fileName)
	{
		System.out.println(url);
		Request request = new Request(url);
		Map<String, Object> tvMap = new HashMap<String, Object>();
		tvMap.put("tvid", "1");
		tvMap.put("tvName", "1");
		tvMap.put("fileName", fileName);
		request.setExtras(tvMap);
		return request;
	}
	
	@Override
	public void process(Page page)
	{
		String type = this.getPageDataType(page);
		switch (type)
		{
			case "data":
				dataProcess(page);
				break;
			case "list":
				listProcess(page);
				break;
			case "forward":
				forwardProcess(page);
				break;
			default:
				break;
		}
	}
	
	public void listProcess(Page page)
	{
		List<String> divList = page.getHtml()
				.xpath("//div[@class='yk-pack pack-film']").all();
		for (String div : divList)
		{
			Html html = new Html(div);
			// String name = html.xpath("//img[@class='quic']/@alt").get();
			// name = StringUtils.isBlank(name) ? "" : name.trim();
			// String set = html.xpath("//span[@class='p-time ']/span/text()")
			// .regex(RegexUtils.REGEX_INTEGER).get();
			// set = StringUtils.isBlank(set) ? "" : set.trim();
			// String count =
			// html.xpath("//ul[@class='info-list']/li[3]/text()")
			// .replace("次播放", "").get();
			// count = StringUtils.isBlank(count) ? "" : count.trim();
			// System.out.println(name + "|" + set + "|" + count + "|" + year);
			// buffer.append(name + "|" + set + "|" + count + "|" + year);
			// buffer.append(SepratorUtil.LINE_SEPRATOR);
			
			PageUtils.addTargetRequest(page,
					html.xpath("//div[@class='p-thumb']/a/@href").get(),
					page.getRequest().getExtras());
		}
		
		String url = page.getHtml()
				.xpath("//div[@class='vaule_main']//li[@class='next']/a/@href")
				.get();
		if (StringUtils.isNotBlank(url))
			PageUtils.addTargetRequest(page, url,
					page.getRequest().getExtras());
		
	}
	
	private void forwardProcess(Page page)
	{
		PageUtils.addTargetRequest(page,
				page.getHtml().xpath("//a[@class='desc-link']/@href").get(),
				page.getRequest().getExtras());
	}
	
	private void dataProcess(Page page)
	{
		String fileName = page.getRequest().getExtra("fileName") + "";
		
		int idx = 10;
		
		Html html = page.getHtml();
		String name = html.xpath("//li[@class='p-row p-title']/text()").get();
		name = StringUtils.isBlank(name) ? "" : name.replace("：", "").trim();
		String set = html.xpath("//li[@class='p-row p-renew']/text()")
				.regex(RegexUtils.REGEX_INTEGER).get();
		set = StringUtils.isBlank(set) ? "" : set.trim();
		String count = html.xpath("//div[@class='p-base']/ul/li[11]/text()")
				.get();
		
		if (count.contains("评论"))
			idx = 9;
		count = count.contains("评论")
				? html.xpath("//div[@class='p-base']/ul/li[10]/text()").get()
				: count;
		count = StringUtils.isBlank(count) ? ""
				: count.replace("总播放数：", "").trim();
		
		String year = html.xpath("//span[@class='sub-title']/text()").get();
		year = StringUtils.isBlank(year) ? "" : year.trim();
		
		List<String> labelList = html
				.xpath("//div[@class='p-base']/ul/li[" + idx + "]/a/text()")
				.all();
		String label = labelList == null || labelList.isEmpty() ? ""
				: StringUtils.join(labelList.toArray(), ",");
		
		System.out.println(
				name + "|" + set + "|" + count + "|" + year + "|" + label);
		
		FileUtil.writeFile(fileName, name + "|" + set + "|" + count + "|" + year
				+ "|" + label + SepratorUtil.LINE_SEPRATOR);
	}
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		return null;
	}
	
	@Override
	protected String getSpiderType()
	{
		return "YKPro";
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		if (page.getUrl().toString()
				.contains("http://list.youku.com/category/show/c_97_a"))
			return "list";
		else if (page.getUrl().toString()
				.contains("http://v.youku.com/v_show/"))
			return "forward";
		else
			return "data";
	}
	
}
